import React,{useState,useEffect} from "react";
import {  ScrollView,Text,View,ImageBackground,Image,Clipboard,Pressable} from 'react-native';
import Headers from '@/app/components/header/Headers';
import { useTranslation } from '@/app/utils/useTranslation';
import {size} from '@/app/utils';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { useNavigation } from "@react-navigation/native";
import { styles } from "./style/styles";
import QRCodeScreen from '@/app/components/QRCodeScreen/index'
const ShareAppScreen = (props:any) => {
    let [opciaty,setOpciaty] = useState(0);
    const { t } = useTranslation();
    let [address,setAddress] = useState('https://register.apexapexapexapexapexapex');
    let [code,setCode] = useState('');
    let navigation = useNavigation<any>();

    useEffect(() => {
        const checkLoginStatus = async () => {
            try {
                const value = await AsyncStorage.getItem('userInfo');
                if (value) {
                    let userInfo = JSON.parse(value);
                    setCode(userInfo.code);
                }else{
                    navigation.reset({
                        index: 0,
                        routes: [{ name: 'pages/auth/LoginScreen' }],
                    });
                }
            } catch (e) {
                console.error('获取用户信息异常', e);
            }
        };
        
        checkLoginStatus();
    }, [navigation]);

    return(
        <ImageBackground style={{ flex: 1,flexDirection:'column'}} source={
          require('@/assets/images/shareapp/1.png')
        }>
            <Headers
                title={t('t428')}
                border={false}
                barStyle={0}
                leftIcon={require('@/assets/images/icon/back.png')}
                rightTitle={t('t422')}
                leftTitle={''}
                backgroundColor={`rgba(255,255,255,${opciaty})`}
                centerColor={'#666'}
                onClickRightBtn={()=>{
                    navigation.navigate('pages/shareApp/MyTeamScreen')
                }}
                {...props}
            />
            <ScrollView 
                onScroll = {(event)=>{{
                    if(event.nativeEvent.contentOffset.y>90){
                        setOpciaty(1);
                    }else{
                        setOpciaty(event.nativeEvent.contentOffset.y/90);
                    }
                }}}
                style={{flex: 1}}
            >
                <View style={{flexDirection:"column",alignItems:'center'}}>
                    <View style={styles.logobox}>
                        <Image
                            source={
                                require('@/assets/images/auth/logo2.png')
                            }
                            style={[styles.IconImage]}
                        />
                    </View>
                <View style={styles.erweimabox}>
                    <View style={styles.touxiang}>
                        <Image style={styles.choutitouxiangbox} source={require('@/assets/images/home/1.png')}/>
                    </View>
                    <View style={{width:size(260),height:size(260),marginTop:size(50)}}>
                        <QRCodeScreen value={'http://boonook.top'} size={size(240)} {...props}/>
                    </View>
                    <Text style={styles.erweimaboxtext}>{t('t42')}</Text>
                    </View>
                    <View style={styles.listitem}>
                        <Text style={styles.listitemleft}>{t('t429')}</Text>
                        <View style={{flex:1}}>
                            <Text style={styles.listaddress} numberOfLines={1}>{address}</Text>
                        </View>
                        <Pressable onPress={()=>{
                            Clipboard.setString(address);
                            global.$toast({message:t('t46')})
                        }}>
                            <Text style={styles.listitemRight}>{t('t47')}</Text>
                        </Pressable>
                         {/* <Pressable onPress={async()=>{
                             try {
                            const result = await Share.share({
                                message: 'Check out this awesome app!',
                                url: 'https://example.com', // iOS only
                                title: 'Awesome App' // Android only
                            });

                            if (result.action === Share.sharedAction) {
                                if (result.activityType) {
                                // Shared with activity type
                                console.log('Shared with', result.activityType);
                                } else {
                                // Shared
                                console.log('Shared successfully');
                                }
                            } else if (result.action === Share.dismissedAction) {
                                // Dismissed
                                console.log('Share dismissed');
                            }
                            } catch (error) {
                                // alert(error.message);
                            }
                        }}>
                            <Text style={styles.listitemRight}>分享</Text>
                        </Pressable> */}
                    </View>
                    <View style={styles.listitem}>
                        <Text style={styles.listitemleft}>{t('t431')}</Text>
                        <View style={{flex:1}}>
                            <Text style={styles.listaddress} numberOfLines={1}>{code}</Text>
                        </View>
                        <Pressable onPress={()=>{
                            Clipboard.setString(code);
                            global.$toast({message:t('t46')})
                        }}>
                            <Text style={styles.listitemRight}>{t('t47')}</Text>
                        </Pressable>
                    </View>
                </View>
            </ScrollView>
        </ImageBackground>
    )
}

export default ShareAppScreen